<template>
  <div>登录</div>
  用户名:<input type="text" v-model="user.name"> <br>
  密码:<input type="password" v-model="user.password"> <br>
  <button @click="login">登录</button>
</template>
<script setup> 
import { useRouter } from 'vue-router'
import {ref} from 'vue'
import { Login } from '../api/auth';
import  {useMenu} from '@/stores/useMenu.js'
const store = useMenu();

const router = useRouter()
const user = ref({name:"",password:""})
const login = ()=>{
  // 调用登录接口
  Login(user.value)
  .then(res=>{
    // code为0表示登录成功
    if(res.data.code==0){
      store.getMenu();
      // 存储token
      sessionStorage.setItem("token",res.data.token)
      sessionStorage.setItem("user",JSON.stringify(res.data.user))
      // 登录成功跳转到后台首页
      router.push({path:"/admin"})
    }else{
      alert(res.data.msg)
    }

  })
  .catch(err=>console.log(err ))
}
</script>